/*
 * BankView.java
 *
 * Created on Dec 18, 2010, 12:29:25 PM
 */
package ui.report;

import ent.IssuedCash;
import ent.IssuedCheque;
import java.awt.Color;
import java.util.Collection;
import util.SuperView;
import java.util.Date;
import java.util.Iterator;
import javax.swing.JFrame;
import javax.swing.table.DefaultTableModel;
import util.Find;

/**
 *
 * @author t
 */
public final class PaymentReportView extends SuperView {

    private static PaymentReportView instance = new PaymentReportView();

    public static void display() {
        instance.setVisible(true);
    }

    public static PaymentReportView getInstance() {
        return instance;
    }

    /**
     * Creates new form BankView
     */
    public PaymentReportView() {
        super();
        onLoad();
    }

    private PaymentReportView(JFrame jFrame, boolean b) {
        this();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        masterScrollPane = new javax.swing.JScrollPane();
        cashTable = new javax.swing.JTable();
        startDatePicker = new org.jdesktop.swingx.JXDatePicker();
        jLabel3 = new javax.swing.JLabel();
        cashTotalLabel = new javax.swing.JLabel();
        endDatePicker = new org.jdesktop.swingx.JXDatePicker();
        jScrollPane1 = new javax.swing.JScrollPane();
        chequeTable = new javax.swing.JTable();
        jLabel1 = new javax.swing.JLabel();
        chequeTableLabel = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("PAYMENT SUMMERY");

        cashTable.setAutoCreateRowSorter(true);
        cashTable.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "#", "Date", "Code", "Supplier", "Invoce", "Bank", "Amount"
            }
        ) {
            boolean[] canEdit = new boolean [] {
                false, false, false, false, false, false, false
            };

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        cashTable.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                cashTableMouseClicked(evt);
            }
            public void mouseReleased(java.awt.event.MouseEvent evt) {
                cashTableMouseReleased(evt);
            }
        });
        cashTable.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                cashTableKeyReleased(evt);
            }
        });
        masterScrollPane.setViewportView(cashTable);
        cashTable.getColumnModel().getColumn(0).setResizable(false);
        cashTable.getColumnModel().getColumn(0).setPreferredWidth(3);
        cashTable.getColumnModel().getColumn(0).setCellRenderer(rightAlignCell);
        cashTable.getColumnModel().getColumn(1).setResizable(false);
        cashTable.getColumnModel().getColumn(1).setPreferredWidth(5);
        cashTable.getColumnModel().getColumn(2).setResizable(false);
        cashTable.getColumnModel().getColumn(2).setPreferredWidth(5);
        cashTable.getColumnModel().getColumn(2).setCellRenderer(rightAlignCell);
        cashTable.getColumnModel().getColumn(3).setResizable(false);
        cashTable.getColumnModel().getColumn(3).setPreferredWidth(200);
        cashTable.getColumnModel().getColumn(4).setResizable(false);
        cashTable.getColumnModel().getColumn(4).setPreferredWidth(5);
        cashTable.getColumnModel().getColumn(5).setResizable(false);
        cashTable.getColumnModel().getColumn(5).setPreferredWidth(5);
        cashTable.getColumnModel().getColumn(6).setResizable(false);
        cashTable.getColumnModel().getColumn(6).setPreferredWidth(5);
        cashTable.getColumnModel().getColumn(6).setCellRenderer(rightAlignCell);

        startDatePicker.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                startDatePickerActionPerformed(evt);
            }
        });

        jLabel3.setText("Date:");

        cashTotalLabel.setFont(new java.awt.Font("Tahoma", 1, 12));
        cashTotalLabel.setHorizontalAlignment(javax.swing.SwingConstants.TRAILING);
        cashTotalLabel.setText("0.00");

        endDatePicker.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                endDatePickerActionPerformed(evt);
            }
        });

        chequeTable.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "#", "Cheque Number", "Bank", "Issued Date", "Banking Date", "amount", "Code", "Name", "Invoice"
            }
        ) {
            boolean[] canEdit = new boolean [] {
                false, false, false, false, false, false, false, false, false
            };

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        jScrollPane1.setViewportView(chequeTable);
        chequeTable.getColumnModel().getColumn(0).setResizable(false);
        chequeTable.getColumnModel().getColumn(0).setPreferredWidth(50);
        chequeTable.getColumnModel().getColumn(1).setResizable(false);
        chequeTable.getColumnModel().getColumn(1).setPreferredWidth(150);
        chequeTable.getColumnModel().getColumn(2).setResizable(false);
        chequeTable.getColumnModel().getColumn(2).setPreferredWidth(150);
        chequeTable.getColumnModel().getColumn(3).setResizable(false);
        chequeTable.getColumnModel().getColumn(3).setPreferredWidth(100);
        chequeTable.getColumnModel().getColumn(4).setResizable(false);
        chequeTable.getColumnModel().getColumn(4).setPreferredWidth(100);
        chequeTable.getColumnModel().getColumn(5).setResizable(false);
        chequeTable.getColumnModel().getColumn(5).setPreferredWidth(100);
        chequeTable.getColumnModel().getColumn(6).setResizable(false);
        chequeTable.getColumnModel().getColumn(6).setPreferredWidth(50);
        chequeTable.getColumnModel().getColumn(7).setResizable(false);
        chequeTable.getColumnModel().getColumn(7).setPreferredWidth(200);
        chequeTable.getColumnModel().getColumn(8).setResizable(false);
        chequeTable.getColumnModel().getColumn(8).setPreferredWidth(100);

        jLabel1.setText("Cash:");

        chequeTableLabel.setFont(new java.awt.Font("Tahoma", 1, 12));
        chequeTableLabel.setText("0.00");

        jLabel2.setText("Cheque:");

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                            .addGroup(javax.swing.GroupLayout.Alignment.LEADING, jPanel1Layout.createSequentialGroup()
                                .addComponent(jLabel3)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(startDatePicker, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(endDatePicker, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addComponent(jLabel1)
                                .addGap(18, 18, 18)
                                .addComponent(cashTotalLabel))
                            .addComponent(masterScrollPane, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 789, Short.MAX_VALUE))
                        .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                            .addComponent(jLabel2)
                            .addGap(18, 18, 18)
                            .addComponent(chequeTableLabel)))
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 789, Short.MAX_VALUE))
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(startDatePicker, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(jLabel3)
                        .addComponent(endDatePicker, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(cashTotalLabel)
                        .addComponent(jLabel1)))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(masterScrollPane, javax.swing.GroupLayout.PREFERRED_SIZE, 229, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(chequeTableLabel)
                    .addComponent(jLabel2))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 244, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(17, Short.MAX_VALUE))
        );

        jPanel1Layout.linkSize(javax.swing.SwingConstants.VERTICAL, new java.awt.Component[] {jScrollPane1, masterScrollPane});

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void cashTableMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_cashTableMouseClicked
    }//GEN-LAST:event_cashTableMouseClicked

    private void cashTableKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_cashTableKeyReleased
    }//GEN-LAST:event_cashTableKeyReleased

    private void cashTableMouseReleased(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_cashTableMouseReleased
    }//GEN-LAST:event_cashTableMouseReleased

    private void startDatePickerActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_startDatePickerActionPerformed
    }//GEN-LAST:event_startDatePickerActionPerformed

    private void endDatePickerActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_endDatePickerActionPerformed
        fill();
    }//GEN-LAST:event_endDatePickerActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                PaymentReportView dialog = new PaymentReportView();
                dialog.addWindowListener(new java.awt.event.WindowAdapter() {
                    @Override
                    public void windowClosing(java.awt.event.WindowEvent e) {
                        System.exit(0);
                    }
                });
                dialog.setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JTable cashTable;
    private javax.swing.JLabel cashTotalLabel;
    private javax.swing.JTable chequeTable;
    private javax.swing.JLabel chequeTableLabel;
    private org.jdesktop.swingx.JXDatePicker endDatePicker;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane masterScrollPane;
    private org.jdesktop.swingx.JXDatePicker startDatePicker;
    // End of variables declaration//GEN-END:variables
    DefaultTableModel cashTableModel;
    DefaultTableModel chequeTableModel;

    private void fillTable() {
        cashTableModel.setRowCount(0);
        chequeTableModel.setRowCount(0);
        Date startDate = startDatePicker.getDate();
        Date endDate = endDatePicker.getDate();
        endDate.setTime(endDate.getTime() + 1000 * 60 * 60 * 24 - 1);
        Collection<IssuedCash> issuedCashs = Find.issuedCashByDates(startDate, endDate);
        if (issuedCashs == null) {
            setStatusMessage("No Cash Record Found!", Color.red);
        }
        int i = 0;
        for (Iterator<IssuedCash> it = issuedCashs.iterator(); it.hasNext();) {
            IssuedCash issuedCash = it.next();
            Object[] row = {
                ++i,
                yyyy_MM_dd.format(issuedCash.getIssuedTime()),
                issuedCash.getPurchaseInvoice().getSupplier().getCode(),
                issuedCash.getPurchaseInvoice().getSupplier().getName(),
                issuedCash.getPurchaseInvoice().getPurchaseInvoicePK().getInvNo(),
                issuedCash.getBank().getName(),
                nf2d.format(issuedCash.getAmount())
            };
            cashTableModel.addRow(row);
        }

        Collection<IssuedCheque> issuedCheques = Find.issuedChequeByDates(startDate, endDate);
        if (issuedCheques == null) {
            setStatusMessage("No Cheque Record Found!", Color.red);
        }
        i = 0;
        for (Iterator<IssuedCheque> it = issuedCheques.iterator(); it.hasNext();) {
            IssuedCheque issuedCheque = it.next();
            Object[] row = {
                ++i,
                issuedCheque.getChequeNumber(),
                issuedCheque.getBank().getName(),
                yyyy_MM_dd.format(issuedCheque.getIssuedDate()),
                yyyy_MM_dd.format(issuedCheque.getBankingDate()),
                nf2d.format(issuedCheque.getAmount()),
                issuedCheque.getPurchaseInvoice().getSupplier().getCode(),
                issuedCheque.getPurchaseInvoice().getSupplier().getName(),
                issuedCheque.getPurchaseInvoice().getPurchaseInvoicePK().getInvNo()
            };
            chequeTableModel.addRow(row);
        }
    }

    protected void onLoad() {
        initComponents();
        cashTableModel = (DefaultTableModel) cashTable.getModel();
        chequeTableModel = (DefaultTableModel) chequeTable.getModel();
    }

    @Override
    public void setVisible(boolean b) {
        super.setVisible(b);
    }

    private void fill() {
        fillTable();
        calcTotal();
    }

    private void calcTotal() {
        double total = 0;
        for (int i = 0; i < cashTableModel.getRowCount(); i++) {
            total += Double.valueOf(cashTableModel.getValueAt(i, 6).toString());
        }
        cashTotalLabel.setText(nf2d.format(total));
        total = 0;
        for (int i = 0; i < chequeTableModel.getRowCount(); i++) {
            total += Double.valueOf(chequeTableModel.getValueAt(i, 5).toString());
        }
        chequeTableLabel.setText(nf2d.format(total));
    }
}
